import java.util.*;

public class BeautyNumberGenerator {

    public static List<String> generateBeautyNumbers(int level, String prefix, int count, Set<String> usedNumbers) {
        List<String> results = new ArrayList<>();
        Set<String> generatedNumbers = new HashSet<>();
        Random random = new Random();
        List<String> patterns = getPatternsByLevel(level);

        int attempts = 0;
        while (results.size() < count && attempts < count * 10) {  // Adding a guard to avoid infinite loops
            String pattern = patterns.get(random.nextInt(patterns.size()));
            String number = generateNumberFromPattern(pattern, prefix, random);

            if (number != null && !usedNumbers.contains(number) && !generatedNumbers.contains(number)) {
                results.add(number);
                generatedNumbers.add(number);
            }
            attempts++;
        }

        return results;
    }

    private static List<String> getPatternsByLevel(int level) {
        List<String> patterns = new ArrayList<>();
        switch (level) {
            case 30:
                patterns.add("######");
                break;
            case 40:
                patterns.add("#####");
                break;
            case 50:
                patterns.add("ABCDB");
                patterns.add("ABCBA");
                patterns.add("ABCAB");
                patterns.add("AABBCC");
                patterns.add("ABAAA");
                patterns.add("AAAABC");
                break;
            case 60:
                patterns.add("AABCB");
                patterns.add("ABCBBB");
                patterns.add("AABCC");
                patterns.add("AABBB");
                patterns.add("AAAABB");
                patterns.add("AAABBB");
                break;
            case 70:
                patterns.add("ABBBC");
                patterns.add("ABBCC");
                patterns.add("AAABC");
                patterns.add("ABCCC");
                patterns.add("AABBC");
                patterns.add("AAAAAB");
                patterns.add("ABBBBB");
                break;
            case 80:
                patterns.add("AAAAB");
                patterns.add("ABAAA");
                patterns.add("AABAA");
                patterns.add("AAABA");
                patterns.add("AAABB");
                patterns.add("AABBB");
                patterns.add("ABBBB");
                patterns.add("ABBBA");
                break;
            case 100:
                patterns.add("####");
                break;
            default:
                throw new IllegalArgumentException("Invalid level: " + level);
        }
        return patterns;
    }

    private static String generateNumberFromPattern(String pattern, String prefix, Random random) {
        Map<Character, Integer> charToDigitMap = new HashMap<>();
        StringBuilder number = new StringBuilder(prefix);

        for (int i = number.length(); i < pattern.length(); i++) {
            char c = pattern.charAt(i);
            if (c == '#') {
                number.append(random.nextInt(10));
            } else if (charToDigitMap.containsKey(c)) {
                number.append(charToDigitMap.get(c));
            } else {
                int digit = random.nextInt(10);
                charToDigitMap.put(c, digit);
                number.append(digit);
            }
        }

        return number.length() == pattern.length() ? number.toString() : null;
    }

    public static void main(String[] args) {
        Set<String> usedNumbers = new HashSet<>(Arrays.asList("12345", "67890"));
        List<String> beautyNumbers = generateBeautyNumbers(100, "1111", 24, usedNumbers);
        System.out.println(beautyNumbers);
    }
}